#include "tore.h"


//Constructeur
Tore::Tore(void)
{
    petitRayon = 0.1f;
    grandRayon = 0.5f;
    NM = NP = 16;
    initialiser();
}

//Contructeur à 4 paramètres
Tore::Tore(float petitRayon, float grandRayon, int NM, int NP)
{
        this->petitRayon = petitRayon;
        this->grandRayon = grandRayon;
        this->NM = NM;
        this->NP = NP;
        initialiser();
}

//Destructeur
Tore::~Tore(void)
{
}

//Creation du tore
void Tore::initialiser()
{
    float phi;
    float theta;

    for(int i = 0; i < NP; ++i)
    {
        phi = (i * PI * 2.0f)/NM;

        for(int j = 0; j < NM; ++j)
        {
            theta = (j * PI * 2.0f)/NP;
            lstPoints.append(new Point((grandRayon + petitRayon * cos(phi))* cos(theta),
                                       (grandRayon + petitRayon * cos(phi)) * sin(theta),
                                       petitRayon * sin(phi)));
        }
    }
}

//Dessine le tore
void Tore::dessiner()
{
    //On dessine le tore tranche par tranche
    for(int i = 0 ; i < NP; ++i)
    {
        glBegin(GL_QUADS);
        for(int j = 0; j < NM; ++j)
        {
            glTexCoord2f(float(i)/(float(NP)), float(j)/(float)(NM));
            glVertex3f(lstPoints[i*NP + j]->getX(), lstPoints[i*NP + j]->getY(), lstPoints[i*NP + j]->getZ());
            glTexCoord2f(float(i)/(float(NP)), float(j+1)/(float)(NM));
            glVertex3f(lstPoints[i*NP + (j+1)%NP]->getX(), lstPoints[i*NP + (j+1)%NP]->getY(), lstPoints[i*NP + (j+1)%NP]->getZ());
            glTexCoord2f(float(i+1)/(float(NP)), float(j+1)/(float)(NM));
            glVertex3f(lstPoints[(i+1)%NP *NP + (j+1)%NP]->getX(), lstPoints[(i+1)%NP *NP + (j+1)%NP]->getY(), lstPoints[(i+1)%NP *NP + (j+1)%NP]->getZ());
            glTexCoord2f(float(i+1)/(float(NP)), float(j)/(float)(NM));
            glVertex3f(lstPoints[(i+1)%NP *NP + j]->getX(), lstPoints[(i+1)%NP *NP + j]->getY(), lstPoints[(i+1)%NP *NP + j]->getZ());
        }
        glEnd();
    }
}
